from PIL import Image
from logs.Logger import Logger
from config import config

# 日志初始化
log = Logger(config.generalConfig.log_path + "/Pic.log", level='info')


def get_image_width(image):
    image = Image.open(image_path)
    width = image.width
    image.close()
    return width

def get_filename(filename):
    return filename.split('.')[0]

def get_extendname(filename):
    return filename.split('.')[1]

def crop_image(image_path, crop_box):
    image = Image.open(image_path)
    cropped_image = image.crop(crop_box)
    image.close()
    return cropped_image


if __name__ == '__main__':
    height_array = [2200, 4355, 6490, 8600, 10565, 12490, 14255, 16001]
    file_name = "20221210182156_81_002.jpg"
    image_path = config.generalConfig.input_path + file_name
    width = get_image_width(image_path)
    h = 0
    w = 0
    step = 1
    extend_name = get_extendname(file_name)
    filename = get_filename(file_name)
    for height in height_array:
        crop_box = (0, h, width, height)
        cropped_image = crop_image(image_path, crop_box)
        print(config.generalConfig.output_path + filename + '_' + str(step) + '.' + extend_name)
        cropped_image.save(config.generalConfig.output_path + filename + '_' + str(step) + '.' + extend_name)
        cropped_image.close()
        h = height
        step = step + 1
